package com.crunch.physics;

import com.crunch.math.Vector2f;

import java.util.Collections;
import java.util.List;

/**
 * Utility methods.
 */
class Helpers {
	public static <T extends Comparable<T>> void sortAndRemoveDuplicates(List<T> list) {
		if (list.isEmpty()) {
			return;
		}

		Collections.sort(list);
		int curr = 0;
		for (int i = 1; i < list.size(); ++i) {
			T a = list.get(curr);
			T b = list.get(i);
			if (a.compareTo(b) != 0) {
				++curr;
				list.set(curr, b);
			}
		}
		while (list.size() > curr+1) {
			list.remove(list.size()-1);
		}
	}

	// transforms p by rotating, then translating
	// rotationVec should contain (cos(angle), sin(angle))
	// if translation is null, no translation is applied
	public static void transformDirect(Vector2f p, Vector2f rotationVec, Vector2f translation) {
		p.set(
				p.x() * rotationVec.x() - p.y() * rotationVec.y(),
				p.x() * rotationVec.y() + p.y() * rotationVec.x());
		if (translation != null) {
			p.addAssign(translation);
		}
	}
}
